package com.xxx.demo.data.command;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.butid.data.codec.CmdMessage;
import com.xxx.demo.data.entity.TDevcmdrec;

import java.util.Date;

/**
 * @author wangjing.dc@qq.com
 * @since 2020/8/12
 */
public class CommandBuilder {

    //                            Unpooled.copiedBuffer("<root><DEVID>11000103</DEVID><TRNTYPE>COMD</TRNTYPE><TRNCODE>60014</TRNCODE>" +
//                                    "<PTIME>2020080714524</PTIME><TRNEJ>0038207725</TRNEJ><COMD_PARA>" +
//                                    "<PARA1>DirInfo</PARA1>" +
//                                    "<PARA2>DirInfo_11000103.txt</PARA2>" +
//                                    "<PARA3>c:\\</PARA3>" +
//                                    "<PARA4></PARA4>" +
//                                    "<PARA5></PARA5>" +
//                                    "<PARA6></PARA6>" +
//                                    "</COMD_PARA></root>", Charset.forName("utf-8")));
    /**
     *
     * @param cmd
     * @return
     */
    public static CmdMessage build(TDevcmdrec cmd) {
        CmdMessage msg = new CmdMessage();
        msg.getHeader()
                .setTermId(cmd.getUntid())
                .setCmdType("COMD")
                .setCmdCode(cmd.getDevcmd())
                .setDateImte(DateUtil.format(new Date(), "yyyyMMddHHmmss"))
                .setServerSeq(IdUtil.fastSimpleUUID());

        msg.addBody("COMD_PARA", StrUtil.split(cmd.getCmdParams(), ','));

        return msg;
    }
}
